#ifndef	_GLOB_H_
#define	_GLOB_H_

// #define DEBUG //
//#define DEBUG_FAULT //
#define FIRSTFIT //BESTFIT 	//or FIRSTFIT
#define SHARED 100
#define RAMSIZE 10000
//#define GRAPHICAL // comment for non-GUI uncomment for GUI

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "op_defines.h"
#include "mesQueue.h"
#include "readyQueue.h"
#include "dList.h"

typedef	u_int32_t	bword;
typedef u_int16_t   binst; //instruction type.
typedef char    bop; //operand type.

#define TRUE	0x01
#define FALSE	0x00
#define	VALID	1
#define	INVALID	0
#define TIMETICKS	6
#define	PAGESIZE	10
#define	NUMPAGES	80
#define	DISKSIZE	10000

typedef struct _PAGETABLE{
	int*	pnPageAddr;
	char*	pchValidness;
} PAGETABLE;

/*Declare memory and registers. */
typedef struct _PCB
{
	FILE*	fpBrain/* = NULL*/;
	char*	reg;
	char*	stackPtr;
	char*	baseReg;
	char*	limitReg;
	char	cFlag;
	char*	IC;
	MSQUEUE*	msgq;
	PAGETABLE*	pagetable;
/* Ready queue and message queues should both be in here too */
} PCB;

/* From FreeMem.h*/
typedef struct _MEMNODE {
	int	base;
	int	end;
	int size;
	struct _MEMNODE* next;
} MEMNODE;

typedef struct _FREEMEMLIST {
	MEMNODE*	head;
} FREEMEMLIST;
/******************/

typedef struct _SEMAPHORE {
	int	value;
	int owner;
	READYQUEUE*	que;
} semaphore;

typedef struct _SHAREDMEM {
	char**	memory;
	semaphore*	semaphores;
} sharedmem;

typedef struct _MEMMOD
{
	char**	ramBlock/* = NULL*/;
	char**	diskBlock;
	size_t	ramSize/* = 0*/;
	size_t	diskSize;
	int*	pnFreePage;
	size_t	nNextPID;
	size_t	nNumProgs;
	size_t  nCurrProc;
	sharedmem*	shared;
	PCB*	pcb;	/* Array of process control blocks, one for each process */
	FREEMEMLIST*	freeMem;
	FREEMEMLIST*	freeDisk;
	DISKLIST* 		usedDisk;
} MEMMOD;

typedef struct _SCHEDUALER
{
	MEMMOD*	mm;
	READYQUEUE*	rdyQ;
	int	timeTick;
	int curProc;
	int numContextSwitches;
	double	sumOfSlices;
} SCHEDUALER;

/* Global helper functions */
int disasm(char instruction[4]);
void diss(unsigned int nFrom, char* pchTo);

#endif	// _GLOB_H_
